package calc1;

import calc1.gun.CalcBaseVisitor;
import calc1.gun.CalcParser;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/6/17.
 */
public class EvalVisitor extends CalcBaseVisitor<Double>{

    public Map<String, Double> vars = new HashMap<>() ;

    // stmt : expr NEWLINE ;
    @Override
    public Double visitPrintExpr(CalcParser.PrintExprContext ctx) {
        Double value = visit(ctx.expr()) ;
        System.out.println(value);
        return .0;
    }

    // stmt : ID '=' expr NEWLINE ;
    @Override
    public Double visitAssign(CalcParser.AssignContext ctx) {
        String id = ctx.ID().getText() ;
        Double val = visit(ctx.expr()) ;
        vars.put(id, val) ;
        return val;
    }

    // expr : '(' expr ')' ;
    @Override
    public Double visitParen(CalcParser.ParenContext ctx) {
        return visit(ctx.expr());
    }

    // expr : expr op=('+'|'-') expr ;
    @Override
    public Double visitAddSub(CalcParser.AddSubContext ctx) {
        double lhs = visit(ctx.expr(0)) ;
        double rhs = visit(ctx.expr(1)) ;
        if (ctx.op.getType() == CalcParser.ADD) {
            return lhs + rhs ;
        }
        return lhs - rhs ;
    }

    // expr : expr op=('*'|'/') expr ;
    @Override
    public Double visitMulDiv(CalcParser.MulDivContext ctx) {
        double lhs = visit(ctx.expr(0)) ;
        double rhs = visit(ctx.expr(1)) ;
        if (ctx.op.getType() == CalcParser.MUL) {
            return lhs * rhs ;
        }
        return lhs / rhs ;
    }

    // expr : NUMBER
    @Override
    public Double visitLiteral(CalcParser.LiteralContext ctx) {
        return Double.valueOf(ctx.NUMBER().getText());
    }
}
